<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>2_对props进行限制</title>
  </head>
  <body>
    <!-- 准备号一个容器 -->
    <div id="test"></div>
    <div id="test2"></div>
    <div id="test3"></div>

    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-dom，用于支持react dom操作 -->
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <!-- 引入prop-types，用于对props进行类型限制 -->
    <script type="text/javascript" src="../js/prop-types.js"></script>
    <!-- 引入babel，用于将jsx转为js -->
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <script type="text/babel">
      // 1. 创建类组件
      class Person extends React.Component {
        render() {
          return (
            <ul>
              <li>姓名：{this.props.name}</li>
              <li>性别：{this.props.gender}</li>
              <li>年龄：{this.props.age + 1}</li>
            </ul>
          )
        }
      }

      // 对标签属性类型、必要性进行限制
      Person.propTypes = {
        name: PropTypes.string.isRequired,
        gender: PropTypes.string,
        age: PropTypes.number
      }

      // 指定标签属性默认值
      Person.defaultProps = {
        gender: '未知',
        age: 18
      }

      // 2. 渲染组件到页面
      ReactDOM.render(<Person name="tom" />, document.getElementById('test'))
      ReactDOM.render(<Person name="jack" gender="男" age={19} />, document.getElementById('test2'))
      const tony = { name: 'tony', gender: '男', age: 20 }
      ReactDOM.render(<Person {...tony} />, document.getElementById('test3'))
    </script>
  </body>
</html>
